#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<fcntl.h>

#define FIFO "/tmp/myfifo"

int main(int argc , char* *argv){

	int fd;
	char buf_r[100];
 	int nread;
	
	if(access(FIFO , F_OK)== -1){
		if( (mkfifo(FIFO, O_CREAT|O_EXCL)<0)&&(errno != EEXIST)  ){
			printf("Can't create fifoserver \n");
			exit(-1);
		}
	}	
	printf("Preparing for reading bytes \n");

	fd = open(FIFO , O_RDONLY|O_NONBLOCK , 0 );	
	
	if( fd == -1)
	{
		perror("Open");
		exit(1);
	}	
	
	while(1){
		memset(buf_r, 0, sizeof(buf_r));
		if((nread=read(fd,buf_r,100))==-1 ){
			if(errno == EAGAIN){
				printf("no data yet \n");
				break;
			}
		
		}else if(nread >= 0)
		{
			printf("read %s from FIFO \n",buf_r);
		}
		sleep(1);
	}
	pause();
	unlink(FIFO);
	return 0;
}
